package com.example.wallpaperapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wallpaperapp.R;
import com.example.wallpaperapp.bean.HomeImageRecyclerViewBean;
import com.example.wallpaperapp.bean.Moment;
import com.example.wallpaperapp.ui.activity.MainPagerActivity;
import com.example.wallpaperapp.ui.fragment.FindAttentionFragment;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import cn.bingoogolapple.baseadapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.baseadapter.BGAViewHolderHelper;
import cn.bingoogolapple.photopicker.widget.BGANinePhotoLayout;

/**
 * @Package: com.example.wallpaperapp.adapter
 * @ClassName: MomentAdapter
 * @Description: findfragment的list适配器
 * @Author: zrmo
 * @CreateDate: 2020/10/26 12:50
 */

public class MainpagerAdapter extends BGARecyclerViewAdapter<Moment> {
    MainpagerAdapter context;
    Bitmap bitmap;
    Bitmap bm;
    Handler handle;

    public MainpagerAdapter(RecyclerView recyclerView, MainpagerAdapter context) {
        super(recyclerView, R.layout.item_mainpager_com);
        this.context = context;
    }


    public Bitmap returnBitMap(final String url) {
        try {
            URL myurl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            conn.getResponseCode();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void fillData(BGAViewHolderHelper helper, int position, Moment moment) {
        if (TextUtils.isEmpty(moment.content)) {
            helper.setVisibility(R.id.find_preview_content, View.GONE);
        } else {
            Thread thread = new Thread(() -> {
                bitmap = returnBitMap(moment.UserPhoto);
                if (!TextUtils.isEmpty(moment.UserPhoto)) {
                    helper.setImageBitmap(R.id.iv_item_moment_avatar, bitmap);
                }
                // 要返回的结果
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            helper.setVisibility(R.id.find_preview_content, View.VISIBLE)
                    .setText(R.id.find_preview_content, moment.content)
                    .setText(R.id.likeNum, moment.likeNum)
                    .setText(R.id.commentNum, moment.commentNum)
                    .setText(R.id.find_preview_username, moment.userName);

        }
        BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);
        ninePhotoLayout.setDelegate((BGANinePhotoLayout.Delegate) context);
        ninePhotoLayout.setData(moment.photos);
    }
}

